{include file='public/header' /}
 <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="{:url('admin/index/main')}">首页</a>
        <a href="{:url('index')}">公告管理</a>
        <a>
          <cite>列表</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:40px">ဂ</i></a>
</div>
<div class="x-body">
<blockquote class="layui-elem-quote">
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="请输入查询内容">
        </div>&nbsp;
     <button class="layui-btn btns" id="search" data-type="reload"><i class="layui-icon" style="color: #fff">&#xe615;</i>搜索</button>
     <button class="layui-btn" style="background:#00BFFF;" onclick="javascript:window.location.href='{:url('add')}'"><i class="layui-icon" style="color: #fff">&#xe654;</i>添加</button>
     <button class="layui-btn" id="delAll" style="background:#FF3030;"><i class="layui-icon" style="color: #fff">&#xe640;</i>批量删除</button>
</blockquote>
<table class="layui-hide" id="list" lay-filter="list"></table>
</div>
<script type="text/html" id="state">
    {{# if(d.state==1){ }}
       <input type="checkbox" name="state" lay-skin="switch" lay-text="开启|关闭" lay-filter="state" checked value="{{d.id}}">
    {{# }else{  }}
       <input type="checkbox" name="state" lay-skin="switch" lay-filter="state" lay-text="开启|关闭" value="{{d.id}}">
    {{# } }}
</script>
<script type="text/html" id="action">
    <a href="{:url('edit')}?id={{d.id}}" title="编辑"><i class="layui-icon" style="color: #5FB878;font-size: 20px;">&#xe642;</i></a>
    <a lay-event="del" title="删除"><i class="layui-icon" style="color:#FF5722;font-size: 20px;">&#xe640;</i></a>
</script>
<script>
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form, $ = layui.jquery;
        var tableIn = table.render({
            elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            page: true,
            cols: [[
                {checkbox:true,fixed: true},
                {field: 'id', title: '编号', width: 80, fixed: true,align: 'center'},
                {field: 'content', title: '内容'},
                {field: 'addtime', title: '添加时间',align: 'center'},
                {title: '启用状态',width:100, align: 'center', toolbar: '#state'},
                {width: 160,title: '操作', align: 'center', toolbar: '#action'}
            ]],
            limit: 10, //每页默认显示的数量
            done:function(msg){
               /* console.log(msg.data);*/
            }
        });

        //修改状态
        form.on('switch(state)', function(obj){
            var urls = "{:url('editstate')}";
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var state = obj.elem.checked===true?1:0;
            $.post(urls,{'id':id,'state':state},function(msg) {
                  layer.close(loading);
                  if(msg.code == 1){
                      layer.msg(msg.msg,{time:1000,icon:6});
                      tableIn.reload();
                  }else{
                      layer.msg(msg.imsg,{time:1000,icon:5});
                }
            },'json');
        });
      
        //搜索
        $('#search').on('click', function() {
            var key = $('#key').val();
            if($.trim(key)==='') {
                layer.msg('{:lang("pleaseEnter")}关键字！',{icon:0});
                return;
            }
            tableIn.reload({
                where: {key: key}
            })
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            console.log(obj.event);
            if (obj.event === 'del') {
                layer.confirm('您确定要删除该记录吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('del')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });

        $('#delAll').click(function(){
            layer.confirm('确认要删除选中信息吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('list'); //test即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('delall')}", {ids: ids}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })
    });
</script>
